//
//  GEShowMutliPopupReportView.m
//  ERP
//
//  Created by QA on 15/11/20.
//  Copyright © 2015年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "GEShowMutliPopupReportView.h"
#import "GEBlurIntroductionView.h"
#import "KLCPopup.h"
#import "KWFormViewQuickBuilder.h"
#import "NSDictionary+DataFormat.h"

@interface GEShowMutliPopupReportView()<UIScrollViewDelegate>

@property (nonatomic, strong) UIView *introductionView;
@property (nonatomic, strong) UIPageControl *PageControl;
@property (nonatomic, strong) KLCPopup* popup;

@end

@implementation GEShowMutliPopupReportView

static GEShowMutliPopupReportView *shareInitializeView;
+ (GEShowMutliPopupReportView*)shareInitialize
{
    if (shareInitializeView) return shareInitializeView;
    shareInitializeView = [[GEShowMutliPopupReportView alloc] init];
    return shareInitializeView;
}

#pragma mark
#pragma mark - IBAction

- (UIView*)drawReportViewName:(NSString*)name titles:(NSArray<NSString*>*)titles data:(NSArray<NSArray<NSString*>*>*)valueArray widths:(NSArray<NSNumber*>*)widths
{
    KWFormViewQuickBuilder *builder = [[KWFormViewQuickBuilder alloc] init];
    [builder setBorderColor:[UIColor colorWithWhite:0.9 alpha:1.0]];
    [builder addRecord:titles];
    
    for (int i=0; i<valueArray.count; i++) {
        [builder addRecord:valueArray[i]];
    }

    UIView *view = [[UIView alloc] init];
    
    //绘制标题
    UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SuperWidth - 20, 30)];
    titleLable.text = name;
    titleLable.font = [UIFont boldSystemFontOfSize:20];
    titleLable.textAlignment = NSTextAlignmentCenter;
    titleLable.textColor = [UIColor colorWithWhite:1.0 alpha:0.85];
    [view addSubview:titleLable];
    
    KWFormView *formView = [builder startCreatWithWidths:widths startPoint:CGPointMake(0, 0)];
    formView.mKWFormViewHeight = 38;
    formView.layer.cornerRadius = 3;
    formView.layer.masksToBounds = YES;
    formView.userInteractionEnabled = NO;
    
    CGRect frame = formView.frame;
    frame.origin.y = 50;
    formView.frame = frame;
    [view addSubview:formView];
    view.frame = CGRectMake(0, 0, formView.bounds.size.width, formView.bounds.size.height + 40);
    return view;
}

- (void)showName:(NSArray<NSString*>*)names titles:(NSArray<NSArray<NSString*>*>*)titles data:(NSArray<NSArray<NSArray<NSString*>*>*>*)valueArray widths:(NSArray<NSArray<NSNumber*>*>*)widths
{
    [self buildIntroName:names titles:titles data:valueArray widths:widths];
    [self show];
}

- (void)show
{
    KLCPopupLayout layout = KLCPopupLayoutMake(KLCPopupHorizontalLayoutCenter, KLCPopupVerticalLayoutCenter);
    KLCPopup* popup = [KLCPopup popupWithContentView:self.introductionView
                                            showType:KLCPopupShowTypeGrowIn
                                         dismissType:KLCPopupDismissTypeShrinkOut
                                            maskType:KLCPopupMaskTypeDimmed
                            dismissOnBackgroundTouch:YES
                               dismissOnContentTouch:NO];
    self.popup = popup;
    [popup showWithTMLayout:layout];
}

#pragma mark - Build MYBlurIntroductionView

-(void)buildIntroName:(NSArray<NSString*>*)names titles:(NSArray<NSArray<NSString*>*>*)titles data:(NSArray<NSArray<NSArray<NSString*>*>*>*)valueArray widths:(NSArray<NSArray<NSNumber*>*>*)widths
{
    //Create Stock Panel with header
    CGRect frame = CGRectMake(0, 0, SuperWidth, SuperHeight);
    UIView *view = [[UIView alloc] initWithFrame:frame];
    self.introductionView = view;
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.contentSize = CGSizeMake(frame.size.width * titles.count, frame.size.height);
    scrollView.delegate = self;
    [view addSubview:scrollView];
    
    for (int i=0; i<titles.count; i++) {
        UIView *view = [self drawReportViewName:names[i] titles:titles[i] data:valueArray[i] widths:widths[i]];
        view.center = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame));
        [scrollView addSubview:view];
        
        CGRect itemFrame = view.frame;
        itemFrame.origin.x = i*frame.size.width + itemFrame.origin.x;
        view.frame = itemFrame;
    }
    
    if (titles.count > 1) {
        UIPageControl *PageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, frame.size.height - 40, frame.size.width, 37)];
        PageControl.userInteractionEnabled = NO;
        PageControl.numberOfPages = titles.count;
        PageControl.currentPage = 0;
        PageControl.pageIndicatorTintColor = [UIColor darkGrayColor];
        PageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        [view addSubview:PageControl];
        self.PageControl = PageControl;
    }
    
    UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap:)];
    [singleTapGestureRecognizer setNumberOfTapsRequired:1];
    [self.introductionView addGestureRecognizer:singleTapGestureRecognizer];
}

-(void)SingleTap:(UITapGestureRecognizer*)recognizer
{
    if (self.popup) [self.popup dismiss:YES];
    shareInitializeView = nil;
}

#pragma mark - UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger currentPanelIndex = scrollView.contentOffset.x/SuperWidth;
    if(self.PageControl) self.PageControl.currentPage = currentPanelIndex;
}

@end
